home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue41 / Patterns / Unit2.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-11-19  |  1.7 KB  |  77 lines

  1. unit Unit2;
  2. //
  3. // Test unit for the generic singleton in HVSingleton and HVTimeKeeper2
  4. //
  5. interface
  6.  
  7. uses
  8.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  9.   StdCtrls;
  10.  
  11. type
  12.   TForm2 = class(TForm)
  13.     Memo: TMemo;
  14.     Button3: TButton;
  15.     GroupBox1: TGroupBox;
  16.     Button2: TButton;
  17.     Button1: TButton;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure Button2Click(Sender: TObject);
  21.     procedure Button3Click(Sender: TObject);
  22.   private
  23.     { Private declarations }
  24.     procedure PollInfo;
  25.   public
  26.     { Public declarations }
  27.   end;
  28.  
  29. var
  30.   Form2: TForm2;
  31.  
  32. implementation
  33.  
  34. {$R *.DFM}
  35.  
  36. uses
  37.   HVTimeKeeper2;
  38.  
  39. // Remove the '.' to test that the statements below will not compile
  40. {.$DEFINE COMPILE_ERRORS_TEST}
  41. procedure TForm2.FormCreate(Sender: TObject);
  42. {$IFDEF COMPILE_ERRORS_TEST}
  43. var
  44.   MyTimeKeeper: TTimeKeeper;
  45. {$ENDIF}
  46. begin
  47. {$IFDEF COMPILE_ERRORS_TEST}
  48.   TimeKeeper := nil;                  // Will not compile
  49.   MyTimeKeeper := TTimeKeeper.Create; // Will not compile
  50.   TimeKeeper.Free;                    // Will not compile
  51.   TimeKeeper.Destroy;                 // Will not compile
  52. {$ENDIF}
  53.   PollInfo;
  54. end;
  55.  
  56. procedure TForm2.Button1Click(Sender: TObject);
  57. begin
  58.   TObject(TimeKeeper).Destroy;        // Will compile, but raises exception at runtime
  59. end;
  60.  
  61. procedure TForm2.Button2Click(Sender: TObject);
  62. begin
  63.   TObject(TimeKeeper).Free;           // Will compile, but raises exception at runtime
  64. end;
  65.  
  66. procedure TForm2.Button3Click(Sender: TObject);
  67. begin
  68.   PollInfo;
  69. end;
  70.  
  71. procedure TForm2.PollInfo;
  72. begin
  73.   Memo.Lines.Add('Current Date and Time is ' + DateTimeToStr(TimeKeeper.Now));
  74. end;
  75.  
  76. end.
  77.